package com.example.controller;

import com.example.DTO.ArticleDTO;
import com.example.DTO.DetailDTO;
import com.example.DTO.SuggestionDTO;
import com.example.pojo.Article;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.service.ArticleService;
import com.example.service.UserService;
import com.example.utils.ThreadLocalUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class AticleController {

    @Autowired
    private UserService userService;

    @Autowired
    private ArticleService articleService;

    @PostMapping("/add")
    public Result add(@RequestBody @Validated Article article){
        if (article.getState().contains("发布")) {
            articleService.add(article);
            return Result.success("已提交，待管理员审核！");
        }
        if (article.getState().equals("草稿")){
            articleService.add(article);
            return Result.success("已保存至草稿箱！");
        }
        return Result.success();
    }

    //文章列表
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer pageNum ,
                       @RequestParam(defaultValue = "5") Integer pageSize,
                       @RequestParam(required = false) Integer categoryId,
                       @RequestParam(required = false) String state){
        PageBean<Article> pageBean = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pageBean);
    }

    //分页查询所有文章列表（在用这个）
    @GetMapping("/listAll")
    public Result listAll(@RequestParam(defaultValue = "1") Integer pageNum ,
                          @RequestParam(defaultValue = "5") Integer pageSize,
                          @RequestParam(required = false) Integer categoryId,
                          @RequestParam(required = false , defaultValue = "已发布") String state){
        PageBean<ArticleDTO> pageBean = articleService.listAll(pageNum,pageSize,categoryId,state);
        return Result.success(pageBean);
    }

    //查询所有文章列表
    @GetMapping("/selectall")
    public Result selectAll(Article article){
        List<ArticleDTO> pageBean = articleService.selectAll(article);
        return Result.success(pageBean);
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer id){
        articleService.delete(id);
        return Result.success("删除成功");
    }
    //获取文章详情
    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id){
        DetailDTO articleDetail = articleService.detail(id);
        return Result.success(articleDetail);
    }
    //更新文章
    @PutMapping("/update")
    public Result update(@RequestBody @Validated Article article){
        if (article.getState().contains("发布")){
            article.setState("待审核");
            articleService.update(article);
            return Result.success("已提交，待管理员审核！");
        }
        if (article.getState().equals("草稿")){
            article.setState("草稿");
            articleService.update(article);
            return Result.success("已保存至草稿箱！");
        }
        return Result.success("更新成功");
    }

    @GetMapping("/mlist")
    public Result articleList(){
      List<Article> list = articleService.blist();
        return Result.success(list);
    }

    //通过文章状态查询文章列表
    @GetMapping("/listByState")
    public Result listByState(@RequestParam String state){
        List<Article> list = articleService.listByState(state);
        return Result.success(list);
    }

    //修改文章的发布状态
    @PutMapping("/updateState")
    public Result updateState(@RequestBody @Validated Article article){
        Claims claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        User user = userService.findByUsername(username);
        if (user.getRole() != 3){
            return Result.error("权限不足");
        }
        articleService.updateState(article);
        return Result.success("操作成功");
    }

    //查询某一篇文章的状态
    @GetMapping("/findState")
    public Result findState(@RequestParam Integer id){
        DetailDTO articleDetail = articleService.detail(id);
        return Result.success(articleDetail.getState());
    }
    //文章搜索建议(关联搜索)
   @GetMapping("/search_suggest")
    public Result suggest(@RequestParam String keyword,@RequestParam(defaultValue = "5", required = false) Integer limit){
        List<SuggestionDTO> list = articleService.suggest(keyword,limit);
        return Result.success(list);
    }

    //文章阅读量增加
    @GetMapping("/addViewCount")
    public Result addViewCount(@RequestParam Integer id){
        articleService.addViewCount(id);
        return Result.success();
    }





}
